package problem135;

import java.util.Arrays;

//135.分发糖果
//https://leetcode.cn/problems/candy/?envType=study-plan-v2&envId=top-interview-150

class Solution {
    public int candy(int[] r) {
        int n = r.length;
        int[] left = new int[n];
        int[] right = new int[n];
        Arrays.fill(left, 1);
        Arrays.fill(right, 1);
        for(int i = 0; i<n-1; i++) {
            if(r[i+1] > r[i]) {
                left[i+1] = left[i]+1;
            } 
        }
        for(int i = n-2; i>=0; i--) {
            if(r[i] > r[i+1]) {
                right[i] = right[i+1] + 1;
            }
        }

        int ret = 0;
        for(int i = 0; i<n; i++) {
            ret += Math.max(left[i], right[i]);
        }
        return ret;
    }
}